package com.tjnmt.togo.activities;


import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.route.BikingRouteResult;
import com.baidu.mapapi.search.route.PlanNode;
import com.baidu.trace.TraceLocation;
import com.tjnmt.togo.BroadcastReceiver.PowerReceiver;
import com.tjnmt.togo.R;
import com.tjnmt.togo.utils.BaiduMapUtil.OpenBaiduMapUtil;
import com.tjnmt.togo.utils.BaiduMapUtil.RoutePlanUtil;
import com.tjnmt.togo.utils.BaiduMapUtil.TraceUtil;
import com.tjnmt.togo.utils.PowerManagerUtil;
import com.tjnmt.togo.utils.ServiceUtil;
import com.tjnmt.togo.utils.overlayutil.BikingRouteOverlay;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subcriber;

import java.util.Timer;
import java.util.TimerTask;

public class TaskActivity extends AppCompatActivity{
    //服务状态判断。
    private Button test;
    private Button search;

    private MapView map_express;
    private BaiduMap bap_express;

    private TraceLocation location = null;

    private IntentFilter intentFilter;
    private PowerReceiver powerReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task_details);

        EventBus.getDefault().register(this);

        PowerManagerUtil.init(this);

        init();


        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                TraceUtil.getLocation();
            }
        },1000,5000);

    }
    public static void actionStart(Context context){

        Intent intent = new Intent(context,TaskActivity.class);
        context.startActivity(intent);

    }
        //初始化配置。
        private void init(){
            test = (Button)findViewById(R.id.test);
            search = (Button)findViewById(R.id.search);

            map_express = (MapView)findViewById(R.id.map_express);
            bap_express = map_express.getMap();
            bap_express.setMapType(BaiduMap.MAP_TYPE_NORMAL);
            bap_express.setCompassPosition(new android.graphics.Point(70, 380));

            test.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(ServiceUtil.isLocating()){
                        Log.e("TAG","成功");
                    }
                    if(location != null){
                        //获取纬度。
                        double latitude = location.getLatitude();
                        //获取经度。
                        double longitude = location.getLongitude();
                        LatLng start = new LatLng(latitude,longitude);
                        LatLng end = new LatLng(39.131106,117.23467800000003);

                        PlanNode stNode = PlanNode.withLocation(start);
                        PlanNode enNode = PlanNode.withLocation(end);

                        RoutePlanUtil.bikeSearch(stNode,enNode);
                    }
                }
            });
            search.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(location != null){

                        double latitude = location.getLatitude();
                        //获取经度。
                        double longitude = location.getLongitude();
                        LatLng start = new LatLng(latitude,longitude);
                        LatLng end = new LatLng(39.131106,117.23467800000003);

                        OpenBaiduMapUtil.startNavigate(start,end);
                    }
                }
            });


             intentFilter = new IntentFilter();
             intentFilter.addAction("android.intent.action.SCREEN_ON");
             intentFilter.addAction("android.intent.action.SCREEN_OFF");
             powerReceiver = new PowerReceiver();
             registerReceiver(powerReceiver,intentFilter);
        }
        //定位当前位置。
        @Subcriber(tag = "tag_localMe")
        private void location(TraceLocation location){
           //Toast.makeText(ApplicationUtils.getContext(),"获得消息",Toast.LENGTH_LONG).show();
            this.location = location;
            //开启定位图层。
            bap_express.setMyLocationEnabled(true);
            // 构造定位数据
            MyLocationData locData = new MyLocationData.Builder()
                    .direction(location.getDirection())
                    .accuracy((float)location.getRadius())
                    .latitude(location.getLatitude())
                    .longitude(location.getLongitude()).build();

            // 设置定位数据
            bap_express.setMyLocationData(locData);
            //设置定位图层配置。
            MyLocationConfiguration config = new MyLocationConfiguration(MyLocationConfiguration.LocationMode.NORMAL,true,null);
            bap_express.setMyLocationConfigeration(config);

           //routePlanSearch.bikingSearch(new BikingRoutePlanOption().from(stNode).to(enNode));
        }
        @Subcriber(tag = "tag_search")
        private void update(BikingRouteResult result){
            BikingRouteOverlay overlay = new BikingRouteOverlay(bap_express);
            bap_express.setOnMarkerClickListener(overlay);
            overlay.setData(result.getRouteLines().get(0));
            overlay.addToMap();
            overlay.zoomToSpan();
        }

        @Override
        protected void onDestroy() {
            super.onDestroy();
            //活动销毁时取消订阅。
            EventBus.getDefault().unregister(this);
            //routePlanSearch.destroy();
            unregisterReceiver(powerReceiver);
        }
}
